package game.logic;
import game.Game;
import game.damageSystem.DamageSystem;
import game.objects.GOPlayer;
import game.objects.GORect;
import game.objects.GameObject;
import game.objects.interfaces.Ownable;
import graphics.objects.GraphicsObject;
import graphics.system.Color;
import java.io.Serializable;

import sound.main.SoundManager;
import tools.Floatmath;
import tools.Point;
import tools.id.IdSystem;
public class GameLogic implements Serializable {
	private Game game;
	private IdSystem<Player> players;
	private transient DamageSystem damage;
	public GameLogic(Game game) {
		this.game = game;
		players = new IdSystem<Player>();
		damage = new DamageSystem(game);
	}
	public void addPlayer(Player player) {
		players.add(player);
	}
	public void remPlayer(Player p) {
		players.remove(p);
	}
	public Player getPlayer(int i) {
		return players.get(i);
	}
	public Player getPlayerById(int id) {
		return players.getById(id);
	}
	public boolean containsPlayerId(int playerId) {
		return players.containsId(playerId);
	}
	public int numPlayers() {
		return players.size();
	}
	public void handle() {
		setCampos();
		for (int i = 0; i < numPlayers(); i++) {
			Player p = players.get(i);
			if (!p.hasFigure()) {
				// TODO : find correct spawn points if there are any.
				GOPlayer gpl = new GOPlayer(game, new Point(0, p.getId() * 100));
				game.addGameObject(gpl);
				p.setFigure(gpl);
			}
		}
		checkTimeActive();
		if (game.server)
			damage.handle();
	}
	private void checkTimeActive() {
		for (int i = 0; i < game.numGameObjects; i++) {
			GameObject go = game.getGameObject(i);
			if (go instanceof TimeActive) {
				if (!((TimeActive) go).isAlive()) {
					game.remGameObject(go);
				}
			}
		}
	}
	private void setCampos() {
		if (players.size() == 0)
			return;
		Player player = players.getById(game.network.id);
		if (player.hasFigure()) {
			game.setCampos(player.getCameraPos());
			SoundManager.setListenerPosition(player.getFigurePos(), new Point());
		}
	}
}
